using System;
using System.Collections;
using System.Collections.Specialized;
using System.Web.UI.WebControls;
using System.Web.UI;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controls.Support.Javascript;

namespace Sedna.Core.Controls.Support
{
	public abstract class BaseContainer : BaseControl, INamingContainer, IClientScriptingControl
	{
		protected IList controlsArray = new ArrayList();
		protected IList cellsArray = new ArrayList();

		public BaseContainer()
		{
		    //TODO: remove
            javascriptClass = "Container";
		}

		public BaseContainer(IConfiguration configuration) : base(configuration)
		{
		}

		protected override void Build()
		{
			base.Build();
			IClientScriptingConfiguration clientScriptingConfiguration=configuration as IClientScriptingConfiguration;
			if (clientScriptingConfiguration!=null)
			{
				ClientScriptingEnabled=clientScriptingConfiguration.ClientScriptingEnabled;
				ClientVariable=clientScriptingConfiguration.ClientVariable;
			}

		}
/*
		protected override void Build()
		{
			base.Build();
			//BuildControlFromConfiguration();
		}
*/
		protected void CreateControls(IContainerLayout containerLayout, IList innerControls)
		{
			containerLayout.PopulateLayout(this, innerControls);
		}


		protected virtual void InsertCellControl(params Control[] controls)
		{
			LayoutCell layoutCell = new LayoutCell();
			for ( int i = 0 ; i < controls.Length ; i++ )
			{
				controlsArray.Add(controls[i]);
				layoutCell.Controls.Add(controls[i]);
			}
			InsertCell(layoutCell);

		}

		protected virtual void InsertCellControl(LayoutCell layoutCell, params Control[] controls)
		{
			for ( int i = 0 ; i < controls.Length ; i++ )
			{
				controlsArray.Add(controls[i]);
				layoutCell.Controls.Add(controls[i]);
			}
			InsertCell(layoutCell);

		}

		protected virtual void InsertCellControl()
		{
			InsertCellControl(1,1);
/*			LayoutCell layoutCell = new LayoutCell();
			LiteralControl nbspLiteralControl = new LiteralControl(null,null,"&nbsp");
			layoutCell.Controls.Add(nbspLiteralControl);
			InsertCell(layoutCell);
*/
		}

		protected virtual void InsertCellControl(int colSpan, int rowSpan)
		{
			LayoutCell layoutCell = new LayoutCell();
			layoutCell.RowSpan=rowSpan;
			layoutCell.ColSpan=colSpan;
			LiteralControl nbspLiteralControl = new LiteralControl(null,null,"&nbsp");
			layoutCell.Controls.Add(nbspLiteralControl);
			InsertCell(layoutCell);

		}


		protected void InsertCellButton( params Control[] controls)
		{
			for ( int i = 0 ; i < controls.Length ; i++ )
			{
			((System.Web.UI.WebControls.Button)controls[i]).Click += new EventHandler(this.ContainerAction);
			}
			InsertCellControl(controls);

		}

		protected void InsertCellButton(LayoutCell layoutCell, params Control[] controls)
		{
			for ( int i = 0 ; i < controls.Length ; i++ )
			{
				System.Web.UI.WebControls.Button button = controls.GetValue(i) as System.Web.UI.WebControls.Button;
				button.Click += new EventHandler(this.ContainerAction);
			}
			InsertCellControl(layoutCell, controls);

		}

		protected virtual void InsertCell(ILayoutCell LayoutCell)
		{
			cellsArray.Add(LayoutCell);

		}

		protected void InsertLabelledControl(Control control,int index,string textKey, string label)
		{
			InsertLabelledControl(control, index, textKey, label, null);

		}

		protected void InsertLabelledControl(Control control,int index,string textKey, string label, string id)
		{
			controlsArray.Add(control);
			((ILabelledControl)control).TextKey = textKey;
			((ILabelledControl)control).Text = label;
			control.ID = id;
			

		}

		protected void InsertButton(Control control,int index,string textKey, string label, string id)
		{
			((System.Web.UI.WebControls.Button)control).Click += new EventHandler(this.ContainerAction);
			InsertLabelledControl(control, index, textKey, label, id);

		}
/*
		public override void FillControlData(string controlId,IDictionary ht)
		{
			string prefix = "";
			if(controlId!=null && controlId != "")
				prefix += controlId + ".";
			for( int i=0; i<controlsArray.Count; i++ )
			{
				Control control = (Control)controlsArray[i];
				if(control == null) continue;
				if(control.ID != null && control.ID != "")
				{
					IControlDataFiller controlDataFiller = (IControlDataFiller)control;
					if (controlDataFiller == null) continue;
					controlDataFiller.FillControlData(prefix + control.ID,ht);
				}
			}
			
		}
*/
		protected void ContainerAction(Object sender, EventArgs e)
		{
//			IDictionary executionContext = new Hashtable();
			IExecutionContext executionContext = ExecutionContext.GetCurrent(ClientID);
//			FillControlData(this.ID,executionContext);
//			IActionExecutor actionExecutor = (IActionExecutor)SpringContextFactory.GetContext().GetObject("runtime.ActionExecutor");
			string actionName = ((Control)sender).ID;
			ControlUtil.ExecuteAction(executionContext, Context, Configuration, this.ClientID, actionName);
//			actionExecutor.ExecuteAction(Context, executionContext, configuration,actionName);
/*			BasePage basePage = this.Page as BasePage;
			if(basePage != null)
			{
				basePage.ActivateFilterState(executionContext);
			}*/
			RaiseBubbleEvent(this, new ModelUpdateEventArgs(executionContext));

		}

		protected override void OnLoad(EventArgs e)
		{
/*		
			if (this.Page.IsPostBack)
			{
				IDictionary executionContext = ExecutionContext.GetCurrent(ClientID);
				FillControlData(this.ID,executionContext);				
			}
*/
			base.OnLoad (e);
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			if (ClientScriptingEnabled)
			{
				EnableClientScripting();
			}
		}

		/// <summary>
		/// Used for javascript generation. Binds controls to corresponding labels.
		/// Filter developer should overload this method if default implementation works bad,
		/// i.e. binds labels to wrong controls.
		/// </summary>
		/// <returns>IDictionary keys are controls' ClientVariables. IDictionary values are corresponding labels' LocalizedTexts .</returns>
//		protected virtual IDictionary BindLabels()
//		{
//			IDictionary binding=new ListDictionary();
//			IClientScriptingControl label=null;
//			foreach (Control control in controlsArray)
//			{
//				if (control is BaseLabel)
//				{
//					label=control as IClientScriptingControl;
//				}
//				else if (control is IClientScriptingControl)
//				{
//					string clientVariable=((IClientScriptingControl)control).ClientVariable;
//					string labelClientVariable=label!=null?label.ClientVariable:null;
//					if (!binding.Contains(clientVariable))
//					{
//						binding.Add(clientVariable, labelClientVariable);
//					}
//					else
//					{
//						if (binding[clientVariable]==null)
//						{
//							binding[clientVariable]=labelClientVariable;	
//						}
//					}
//					label=null;				
//				}
//				
//			}
//			return binding;
//		}

		protected virtual void EnableClientScripting()
		{
//			EnableChildrenScripting();
//
//			JavascriptController controller=JavascriptControllerLocator.Locate();
//
//			controller.DeclareClass("Container");
//			IDictionary binding=BindLabels();
//			controller.DeclareObject(ClientVariable, "Container", new ScriptObjectGenerator(binding.Keys, false), new ScriptObjectGenerator(binding.Values, false) );
            JavascriptController.Current.DeclareObject(ClientVariable, "Container");
		}
//
//		protected void EnableChildrenScripting()
//		{
//			foreach (Control control in controlsArray)
//			{
//				IClientScriptingControl scriptingControl=control as IClientScriptingControl;
//				if (scriptingControl!=null)
//				{
//					scriptingControl.ClientScriptingEnabled=true;
//				}
//			}	
//		}


	}
}
